<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8" />
	<title>Loaha CMS</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
	<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
	<link rel="stylesheet" href="/app/laoha/sdk/sdk.css" />
	<link rel="stylesheet" href="/app/laoha/sdk/helper.css" />
	<link rel="stylesheet" href="/app/laoha/sdk/iconfont.css" />
	<style>
		html,
		body,
		.app-wrapper {
			position: relative;
			width: 100%;
			height: 100%;
			margin: 0;
			padding: 0;
		}

		img {
			max-width: 100%;
		}
	</style>
</head>

<body>
	<div id="root" class="app-wrapper"></div>
	<script src="/app/laoha/sdk/sdk.js"></script>
	<script type="text/javascript">
		// API相关常量
		const GROUP_API = "/app/laoha/admin/post/groups";
		const NEW_API = "/app/laoha/admin/post/create";
		const LIST_API = "/app/laoha/admin/post/list";
		const EDIT_API = "/app/laoha/admin/post/edit";
		const SHOW_API = "/app/laoha/admin/post/show";
		const CHECK_API = "/app/laoha/admin/post/check";
		const DELETE_API = "delete:/app/laoha/admin/post/delete";
		const UPDATE_API = "put:/app/laoha/admin/post/update";
		(function () {
			let amis = amisRequire('amis/embed');
			// 通过替换下面这个配置来生成不同页面
			let amisJSON = {
				"type": "page",
				"data": <?= json_encode($params, JSON_UNESCAPED_UNICODE) ?>,
				"title": "文章管理",
				"body": [
					{
						"type": "crud",
						"autoGenerateFilter": {
							"columnsNum": 2,
							"defaultCollapsed": false,
							"showBtnToolbar": false
						},
						"api": LIST_API,
						"defaultParams": {
							"perPage": 10,
							"orderBy": 'id',
							"orderDir": 'desc'
						},
						"headerToolbar": [
							//新增
							{
								"label": "新增",
								"type": "button",
								"level": "primary",
								"actionType": "link",
								"blank": false,
								"link": NEW_API,
								"align": "right",
								"visibleOn": "${rules.store || rules.admin}"
							},
							//批量操作
							{
								"type": "bulkActions",
								"visibleOn": "${rules.admin || rules.manage}"
							}

						],
						//批量操作
						"bulkActions": [
							{
								"label": "批量删除",
								"actionType": "ajax",
								"api": DELETE_API + "?ids=${ids}",
								"confirmText": "确定要批量删除?",
								"visibleOn": "${rules.admin || (rules.delete && rules.manage)}",
							},
							{
								"label": "批量审核",
								"actionType": "dialog",
								"visibleOn": "${rules.admin || rules.manage}",
								"dialog": {
									"title": "批量审核",
									"body": {
										"type": "form",
										"api": CHECK_API,
										"body": [
											{
												"type": "hidden",
												"name": "ids"
											},
											{
												"type": "select",
												"name": "status",
												"label": "更改状态",
												"options": [
													{
														"label": "待审",
														"value": 1
													},
													{
														"label": "已审",
														"value": 0
													}
												]
											},
										]
									}
								}
							},
							{
								"label": "批量分类分组",
								"actionType": "dialog",
								"visibleOn": "${rules.admin || (rules.update && rules.manage)}",
								"dialog": {
									"title": "批量编辑",
									"body": {
										"type": "form",
										"api": UPDATE_API,
										"body": [
											{
												"type": "hidden",
												"name": "ids"
											},
											{
												"type": "tree-select",
												"name": "cate_id",
												"label": "选择分类",
												"onlyLeaf": true,
												"hideNodePathLabel": true,
												"labelField": "title",
												"valueField": "id",
												"source": "${cates}",
												"required": true
											},
											{
												"type": "select",
												"name": "group",
												"label": "选择分组",
												"source": GROUP_API + "?id=${cate_id}",
												"visibleOn": "${config.groups}"
											},
										]
									}
								}
							},
							{
								"label": "批量位置",
								"visibleOn": "${rules.admin || (rules.update && rules.manage)}",
								"actionType": "dialog",
								"dialog": {
									"title": "批量编辑位置",
									"body": {
										"type": "form",
										"api": UPDATE_API,
										"body": [
											{
												"type": "hidden",
												"name": "ids"
											},
											{
												"type": "input-tag",
												"name": "pos",
												"label": "选择位置",
												"source": "${config.poses}",
											},
										]
									}
								}
							},
						],
						"rowClassNameExpr": "<%= data.status ? 'bg-light':'' %>",
						//列表
						"columns": [
							{
								"name": "id",
								"label": "ID",
								"type": "text",
								"sortable": true
							},
							{
								"name": "cover",
								"label": "封面",
								"type": "image",
								"thumbMode": "cover",
								"width": "45px",
								"height": "45px",
								"enlargeAble": true,
							},
							{
								"name": "title",
								"label": "标题",
								"width": "30%",
								"type":"tpl",
								"tpl":"<h3 class='mb-2'>${cate.is_hidden==1 ? '【隐】':''} ${cate.status==1 ? '<a href=\"/app/laoha/post/detail?id='+id+'\" target=\"_blank\"><s>'+title+'</s></a>':'<a href=\"/app/laoha/post/detail?id='+id+'\" target=\"_blank\">'+title+'</a>'}</h3> <span class='text-xs text-secondary'>${pos}</span>",
								"searchable": {
									"type": "input-text",
									"name": "title",
									"label": "标题",
									"clearable": true,
									"placeholder": "输入标题"
								}
							},
							{
								"name": "pos",
								"label": "位置",
								"searchable": {
									"type": "select",
									"name": "pos",
									"label": "位置",
									"placeholder": "选择位置",
									"clearable": true,
									"source": "${config.poses}",
									"visibleOn": "${config.poses}"
								},
								"toggled": false,
								"visibleOn": "${config.poses}"
							},
							{
								"name": "cate_id",
								"label": "分类",
								"type": "tpl",
								"tpl": "<a href='/app/laoha/admin/post?cate_id=${cate_id}'>${cate.status==1 ? '<s>'+cate.title+'</s>':cate.title}</a> ${cate.is_hidden==1 ? '【隐】':''}",
								"searchable": {
									"type": "tree-select",
									"name": "cate_id",
									"label": "分类",
									"placeholder": "选择分类",
									"onlyLeaf": true,
									"hideNodePathLabel": true,
									"labelField": "title",
									"valueField": "id",
									"source": "${cates}",
								}
							},
							{
								"name": "group",
								"label": "分组",
								"visibleOn": "${config.groups}"
							},
							{
								"name": "admin_id",
								"label": "发表",
								"type": "tpl",
								"tpl": "<a href='/app/laoha/admin/post?admin_id=${admin_id}'>${admin.nickname}</a>",
							},
							{
								"name": "published_at",
								"label": "日期",
								"type": "date",
								"fromNow": true,
								"sortable": true
							},
							{
								"name": "status",
								"label": "状态",
								"type": "status",
								"labelMap": {
									"1": "待审",
									"0": "${cate.status!==0 ? '无分类':'✔'}"
								},
								"map": {
									"0": "✔"
								},
								"filterable": {
									"options": [
										{
											"label": "待审",
											"value": 1
										},
										{
											"label": "已审",
											"value": 0
										}
									]
								}
							},
							{
								"type": "operation",
								"label": "操作",
								"width": 100,
								"fixed": "right",
								"buttons": [
									{
										"type": "button",
										"icon": "fa fa-eye",
										"tooltip": "查看",
										"type": "link",
										"blank": true,
										"href":"/app/laoha/post/detail?id=${id}",
										"body": " ",
									},
									//编辑
									{
										"type": "button",
										"icon": "fa fa-pencil",
										"tooltip": "编辑",
										"type": "link",
										"blank": false,
										"href": EDIT_API + "?id=${id}",
										"body": " ",
										"title": "编辑",
										"visibleOn": "${rules.admin || rules.manage || (status && cur_admin_id==admin_id)}",
									},
									//删除
									{
										"type": "button",
										"icon": "fa fa-times text-danger",
										"tooltip": "删除",
										"actionType": "ajax",
										"confirmText": "您确认要删除?",
										"api": DELETE_API + "?id=${id}",
										"visibleOn": "${rules.admin || rules.manage || (status && cur_admin_id==admin_id)}",
									}
								]
							}
						]
					}]
			};
			let amisScoped = amis.embed('#root', amisJSON, {}, { toastPosition: 'center' });
		})();
	</script>
</body>

</html>